package com.brooks.demo;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock 公平锁
 *
 * @Author mihutu
 * @Date 2020/10/18 21:03
 * @Version 1.0
 */
public class DemoReentrantLock4 extends Thread {
   private static ReentrantLock lock = new ReentrantLock(true);

    @Override
    public void run() {
        for (int i=0;i<100;i++){
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"获得锁");
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        DemoReentrantLock4 rl = new DemoReentrantLock4();
        Thread thread1 = new Thread(rl);
        Thread thread2 = new Thread(rl);
        thread1.start();
        thread2.start();
    }
}